package com.gifcpp.play;

import android.graphics.Bitmap;
import android.os.Bundle;
import android.os.Environment;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

import java.io.File;

import androidx.appcompat.app.AppCompatActivity;

public class MainActivity extends AppCompatActivity {


    private GifHandler mGifHandler;
    private Bitmap mBitmap;
    private ImageView mImageView;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);
        mImageView = findViewById(R.id.debug_img);

        loadGif(null);
    }


    public void loadGif(View view) {


        File file = new File(Environment.getExternalStorageDirectory(), "demo1.gif");
        if (!file.exists()) {
            return;
        }
        mGifHandler = GifHandler.load(file.getPath());
        int width = mGifHandler.getWidth();
        int height = mGifHandler.getHeight();
        Log.i("123123", "w:" + width + "-h:" + height);
        mBitmap = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        render();

    }

    private Handler mHandler = new Handler();

    private void render() {
        int delay = mGifHandler.updateFrame(mBitmap);
        if (delay < 0) {
            return;
        }
        mImageView.setImageBitmap(mBitmap);
        mHandler.postDelayed(new Runnable() {
            @Override
            public void run() {
                render();
            }
        }, delay);
    }

    public void high(View view) {
        mGifHandler.hight();
    }

    public void low(View view) {
        mGifHandler.debug();
        Button button= (Button) view;
        button.setText(mGifHandler.mTextString);
    }
}
